package com.jlnk.rxjavademo2.view;

import android.os.Bundle;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.jlnk.rxjavademo2.R;
import com.jlnk.rxjavademo2.adapter.NewsAdapter;
import com.jlnk.rxjavademo2.entity.NetworkHotEntity;
import com.jlnk.rxjavademo2.presenter.NetworkHotPresenter;

public class NetworkHotActivity extends AppCompatActivity implements NetworkHotView {
    private ProgressBar progressBar;
    private RecyclerView recyclerView;
    private NewsAdapter adapter;
    private NetworkHotPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network_hot);
        progressBar = findViewById(R.id.progressBar);
        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new NewsAdapter();
        recyclerView.setAdapter(adapter);
        presenter = new NetworkHotPresenter(this);
        presenter.loadNetworkHot();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unSubscribe();
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(ProgressBar.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(ProgressBar.GONE);
    }

    @Override
    public void showNetworkHotItem(NetworkHotEntity.Result result) {
        if (result != null && result.getList() != null) {
            adapter.updateItems(result.getList());
        }
    }

    @Override
    public void showError(String message) {
    }
}